<?php

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 * Description of account
 *
 * @author b4by
 */
class account extends MY_Controller{
	function __construct() {
		parent::__construct();
	}
	
	public function index()
	{
	}
	
	public function register()
	{
		$this->load->helper('form');
		$this->load->library('form_validation');
		
		$this->form_validation->set_rules('username','lang:username','required|min_length[5]|max_length[50]');
		$this->form_validation->set_rules('password','lang:password','required|min_length[5]|max_length[30]');
		$this->form_validation->set_rules('password1','lang:password_confirm','required|matches[password]');
		$this->form_validation->set_rules('mobile','lang:mobile','required|min_length[8]|max_length[20]');
		$this->form_validation->set_rules('fullname','lang:fullname','min_length[8]|max_length[100]');
		$this->form_validation->set_rules('address','lang:address','min_length[8]');
		$this->form_validation->set_rules('email','lang:email','required|valid_email|callback__check_email');
		$this->form_validation->set_rules('agree','lang:agree_terms','required|callback__check_agree_terms');
//		$this->form_validation->set_rules('gender','lang:gender','callback__check_gender');
		
		
		if($this->form_is_valid()){
			$data = request_filter(array('username','password','mobile','email','address'));
			$data['password'] = md5($data['password']);
			$data['active']	= 1;
			$this->model_user->create($data);
			
			echo json_encode(array(
				'state'	=> 'success',
				'message'	=> lang('registration_success'),
			));
		}else{
			echo json_encode(array(
				'state'	=> 'error',
				'errors'	=> $this->data['errors']
			));
		}
		
	}
	
	public function _check_email($email)
	{
		$info = $this->model_user->get_info_rule(array(
			'email'	=> $email,
		));
		if($info===FALSE)
			return TRUE;
		$this->form_validation->set_message(__FUNCTION__,lang('email_used'));
		return FALSE;
	}
	public function _check_agree_terms($value)
	{
		if($value==1) return TRUE;
		$this->form_validation->set_message(__FUNCTION__,lang('agree_terms_notify'));
		return FALSE;
	}
	
	public function login()
	{
		$this->load->helper('form');
		$this->load->library('form_validation');
		
		$this->form_validation->set_rules('username','lang:username','required');
		$this->form_validation->set_rules('password','lang:password','required|callback__check_login');
		
		if($this->form_is_valid())
		{
			echo json_encode(array(
				'state'	=> 'success',
				'user'	=> '',
			));
		}
		else{
			$data = array(
				'state'	=> 'error',
				'errors'	=> $this->data['errors'],
			);
			echo json_encode($data);
		}
	}
	
	public function _check_login($value)
	{
		if(user_login($this->input->post('username'), $this->input->post('password'), $this->input->post('remember')))
			return TRUE;
		$this->form_validation->set_message(__FUNCTION__,lang('login_fail'));
		return FALSE;
	}
	
	public function fb_login() {

		$ref = request('ref');

		if ($ref == '')
			$ref = url_create('site');

		if (preg_match('/' . addcslashes(url_create('site', 'account', 'fb_login'), '/.\\*+[])(|?^') . '/i', $ref) != 0)
			$ref = url_create('site');

		if ($this->isUserLogin())
			redirect($ref);

		$this->load->library('facebook');
		$this->load->model('model_user');

		$code = request('code');

		if (strlen($code) == 0) {
			redirect($this->facebook->getLoginUrl(array(
						'scope' => 'email,user_about_me'
					)));
		}
		$graph_url = "https://graph.facebook.com/me?access_token="
				. $this->facebook->getAccessToken();

		$user = json_decode(file_get_contents($graph_url));

		$fb_userId = $user->id;

		if ($fb_userId == 0) {
			echo 'Not authentication';
			exit;
		}
		$info = $this->model_user->get_info_rule(array('fb_id' => $fb_userId));

		if ($info !== false) {
			$this->model_user->update($info->id, array(
				'username' => $user->username,
				'fullname' => $user->name,
			));
			$this->session->set_userdata('user_id', $info->id);
			redirect($ref);
		}

		$user_id = $this->model_user->create(array(
			'email' => $user->email,
			'username' => $user->username,
			'fb_id' => $fb_userId,
			'active' => 1,
			'group_id' => $this->siteConfig->default_user_group,
			'fullname' => $user->name,
			'join_date' => now()
				));

		$this->session->set_userdata('user_id', $user_id);

		redirect($ref);
	}

	public function logout() {
		$this->session->set_userdata('user_id', $this->siteConfig->guest_id);
		set_cookie('username', '');
		set_cookie('password', '');
		redirect(url_create('site'));
	}
}

?>
